<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
    <title>ShellCheck: SC2003 – expr is antiquated. Consider rewriting this using `$((..))`, `${}` or `[[  ]]`.</title>
    <link rel="stylesheet" href="css/bootstrap.min.css" />
  </head>
  <body style="margin-left: auto; margin-right: auto; max-width: 800px">
    <h1>SC2003 – ShellCheck Wiki</h1>
    <a href="https://github.com/koalaman/shellcheck/wiki/SC2003">See this page on GitHub</a>
    <p style="display: none"><a href="index.html">Sitemap</a></p>
    <hr />
    <h1 id="expr-is-antiquated-consider-rewriting-this-using---or---">expr
is antiquated. Consider rewriting this using <code>$((..))</code>,
<code>${}</code> or <code>[[  ]]</code>.</h1>
<h3 id="problematic-code">Problematic code:</h3>
<div class="sourceCode" id="cb1"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb1-1"><a href="SC2003.html#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="va">i</span><span class="op">=</span><span class="va">$(</span><span class="fu">expr</span> 1 + 2<span class="va">)</span></span>
<span id="cb1-2"><a href="SC2003.html#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="va">l</span><span class="op">=</span><span class="va">$(</span><span class="fu">expr</span> length <span class="st">&quot;</span><span class="va">$var</span><span class="st">&quot;</span><span class="va">)</span></span></code></pre></div>
<h3 id="correct-code">Correct code:</h3>
<div class="sourceCode" id="cb2"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb2-1"><a href="SC2003.html#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="va">i</span><span class="op">=</span><span class="va">$((</span><span class="dv">1</span><span class="op">+</span><span class="dv">2</span><span class="va">))</span></span>
<span id="cb2-2"><a href="SC2003.html#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="va">l</span><span class="op">=</span><span class="va">${</span><span class="op">#</span><span class="va">var}</span></span></code></pre></div>
<p><strong>WARNING:</strong> constants with a leading 0 are interpreted
as octal numbers by bash, but not by expr. Then you should specify the
base when a leading zero may occur:</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb3-1"><a href="SC2003.html#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> x=08</span>
<span id="cb3-2"><a href="SC2003.html#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> echo <span class="va">$(</span><span class="fu">expr</span> 1 + <span class="va">$x)</span></span>
<span id="cb3-3"><a href="SC2003.html#cb3-3" aria-hidden="true" tabindex="-1"></a><span class="ex">9</span></span>
<span id="cb3-4"><a href="SC2003.html#cb3-4" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> echo <span class="va">$((</span><span class="dv">1</span> <span class="op">+</span> <span class="va">$x))</span></span>
<span id="cb3-5"><a href="SC2003.html#cb3-5" aria-hidden="true" tabindex="-1"></a><span class="ex">-bash:</span> 1 + 08: value too great for base <span class="er">(</span><span class="ex">error</span> token is <span class="st">&quot;08&quot;</span><span class="kw">)</span></span>
<span id="cb3-6"><a href="SC2003.html#cb3-6" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> echo <span class="va">$((</span><span class="dv">1</span> <span class="op">+</span> <span class="dt">10#</span><span class="va">$x))</span></span>
<span id="cb3-7"><a href="SC2003.html#cb3-7" aria-hidden="true" tabindex="-1"></a><span class="ex">9</span></span></code></pre></div>
<p>See <a
href="https://github.com/koalaman/shellcheck/issues/1910#issuecomment-610439789">issue
#1910</a></p>
<h3 id="rationale">Rationale:</h3>
<p><a
href="https://pubs.opengroup.org/onlinepubs/009695399/utilities/expr.html#tag_04_50_17">To
quote POSIX:</a></p>
<blockquote>
<p>The expr utility has a rather difficult syntax [...] In many cases,
the arithmetic and string features provided as part of the shell command
language are easier to use than their equivalents in expr. Newly written
scripts should avoid expr in favor of the new features within the
shell.</p>
</blockquote>
<h3 id="exceptions">Exceptions</h3>
<p><code>sh</code> doesn't have a great replacement for the
<code>:</code> operator (regex match). ShellCheck tries not to warn when
using expr with <code>:</code>, but e.g.
<code>op=:; expr string "$op" regex</code> will still trigger it.</p>
<p>Other than that, all uses of <code>expr</code> can be rewritten to
use modern shell features instead.</p>
<p>Bash has <code>[[ string =~ regex ]]</code>, so not even
<code>expr .. : ..</code> is necessary.</p>
    <hr />
    <p style='font-size: 80%'><a href="../index.html">ShellCheck</a> is a static analysis tool for shell scripts. This page is part of its documentation.</p>
  </body>
</html>


